package com.itheima.practice;

public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = {2, 9, 12, 23, 34, 45, 56};
        int number = 23;
        int result = binarySearch(arr,number);
        System.out.println(result);
    }

    private static int binarySearch(int[] arr,int number) {
        int index = -1;
        int min = 0;
        int max = arr.length-1;
        while (max > min){
            int mid = (min + max)>>1;
            if(arr[mid] > number){
                max = mid - 1;
            }else if (arr[mid] < number) {
                min = mid + 1;
            }else {
                return mid;
            }
        }
        return index;
    }
}
